package com.feng.code.utils;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feng.code.constant.UserConstant;
import com.feng.code.exception.BusinessException;
import com.feng.code.exception.ErrorCode;
import com.feng.code.pojo.dto.admin.UserQueryRequest;
import com.feng.code.pojo.entity.User;
import com.feng.code.pojo.enums.UserRoleEnum;
import com.feng.code.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * User自定义工具类
 */
public class UserUtils {

    /**
     * 密码加密
     * @param userPassword 用户密码
     * @return 加密之后的密码
     */
    public static String getEncryptPassword(String userPassword) {
        // 盐值，混淆密码
        return DigestUtils.md5DigestAsHex((UserConstant.SALT + userPassword).getBytes());
    }

    /**
     * 判断是否为管理员
     * @param user 当前用户
     * @return 是否为管理员
     */
    public static boolean isAdmin(User user) {
        return user != null && UserRoleEnum.ADMIN.getValue().equals(user.getUserRole());
    }

    /**
     * 用户封装类 --> 用户实体类
     * @param userVO 用户封装对象
     * @return 用户实体对象
     */
    public static User voToObj(UserVO userVO) {
        if (userVO == null) {
            return null;
        }
        User user = new User();
        BeanUtils.copyProperties(userVO, user);
        return user;
    }

    /**
     * 用户实体类 --> 用户封装类
     * @param user 用户实体对象
     * @return 用户封装对象
     */
    public static UserVO objToVo(User user) {
        if (user == null) {
            return null;
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return userVO;
    }

    /**
     * 用户实体类列表 --> 用户封装类列表
     * @param userList 用户实体类列表
     * @return 用户封装类列表
     */
    public static List<UserVO> getUserVoList(List<User> userList) {
        if (CollUtil.isEmpty(userList)) {
            return new ArrayList<>();
        }
        return userList.stream().map(UserUtils::objToVo).collect(Collectors.toList());
    }

    /**
     * 用户查询 --> QueryWrapper 查询对象
     * @param userQueryRequest 用户查询请求体
     * @return QueryWrapper 查询对象
     */
    public static QueryWrapper<User> getQueryWrapper(UserQueryRequest userQueryRequest) {
        if (userQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }

        Long id = userQueryRequest.getId();
        String userAccount = userQueryRequest.getUserAccount();
        String userName = userQueryRequest.getUserName();
        String userProfile = userQueryRequest.getUserProfile();
        String userRole = userQueryRequest.getUserRole();
        String sortField = userQueryRequest.getSortField();
        String sortOrder = userQueryRequest.getSortOrder();

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

// 只有在参数非空时才拼接条件（避免全表扫描或 where id = null）
        if (id != null) {
            queryWrapper.eq("id", id);
        }
        if (userRole != null && !userRole.isEmpty()) {
            queryWrapper.eq("userRole", userRole);
        }
        if (userAccount != null && !userAccount.isEmpty()) {
            queryWrapper.like("userAccount", userAccount);
        }
        if (userName != null && !userName.isEmpty()) {
            queryWrapper.like("userName", userName);
        }
        if (userProfile != null && !userProfile.isEmpty()) {
            queryWrapper.like("userProfile", userProfile);
        }

// 排序
        if (sortField != null && !sortField.isEmpty()) {
            boolean isAsc = "ascend".equalsIgnoreCase(sortOrder);
            if (isAsc) {
                queryWrapper.orderByAsc(sortField);
            } else {
                queryWrapper.orderByDesc(sortField);
            }
        }

        return queryWrapper;

    }

}


